from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from goods.models import SpecificationOption, SPUSpecification
from meiduo_admin.serializers.options import SpecificationOptionSerializer
from meiduo_admin.serializers.skus import SPUSpecificationSerializers

from meiduo_admin.utils import PageNum, OptionPermission


class OtionsView(ModelViewSet):
    serializer_class =SpecificationOptionSerializer
    queryset = SpecificationOption.objects.all()
    pagination_class = PageNum
    permission_classes = [IsAdminUser]
    permission_classes_by_action = {'create': [OptionPermission],
                                    'update': [OptionPermission],
                                    'destroy': [OptionPermission]
                                    }

    def get_permissions(self):
        try:
            # return permission_classes depending on `action`
            return [permission() for permission in self.permission_classes_by_action[self.action]]
        except KeyError:
            # action is not set return default permission_classes
            return [permission() for permission in self.permission_classes]

    def simple(self,request):
        # 获取spu规格表信息

        specs = SPUSpecification.objects.all()
        ser = SPUSpecificationSerializers(specs, many=True)
        return Response(ser.data)